<?php


namespace app\index\controller;


use app\common\controller\CommonController;
use app\common\exception\NotFoundException;
use app\common\exception\ParameterFailedException;
use app\common\middleware\AuthToken;
use app\common\model\MediaModel;
use app\common\model\ProductCategoryModel;
use app\common\model\ProductModel;
use app\common\service\Lang;
use app\common\validate\ProductValidate;
use think\facade\Config;
use think\Request;

class ProductController extends CommonController
{
    protected $middleware = [
        AuthToken::class => [
            'except' => [
                'getProduct',
                'getProductByIds',
                'getProductCategoryList',
                'getProductList',
            ],
        ],
    ];

    public function getProduct(Request $request)
    {
        $params = self::getParam(['product_id']);
        (new ProductValidate())->goCheck(__FUNCTION__, $params);
        $model = (new ProductModel())->getProduct([
            [
                'id',
                '=',
                $params['product_id'],
            ],
        ]);
        if ($model) {
            $modelArr = $model->toArray();
            self::arrangeProduct($modelArr);
        }
        else {
            throw new NotFoundException();
        }

        return retJson(Lang::GETDone, $modelArr);
    }

    public function getProductByIds(Request $request)
    {
        $params = self::getParam(['ids']);
        (new ProductValidate())->goCheck(__FUNCTION__, $params);
        $ids    = explode(',', $params['ids']);
        $models = (new ProductModel())->getProduct([
            [
                'id',
                'in',
                $ids,
            ],
        ]);
        if ($models) {
            $modelsArr  = $models->toArray();
            $modelsData = $modelsArr['data'];
            self::arrangeProduct($modelsData, true);
            //        var_dump($modelsArr);die;
            $modelsArr['data'] = $modelsData;
        }
        else {
            $modelsArr['data'] = [];
        }
        return retJson(Lang::GETDone, $modelsArr);
    }

    public function getProductCategoryList(Request $request)
    {
        $searchParams = self::getParam([]);
        $where        = [];
        foreach ($searchParams as $key => $value) {
            if (is_array($value) && $key === 'create_time') {
                $where[] = [
                    $key,
                    'between',
                    $value,
                ];
                continue;
            }
            if (strpos($value, '%') !== false) {
                $where[] = [
                    $key,
                    'like',
                    $value,
                ];
            }
            else {
                $where[] = [
                    $key,
                    '=',
                    $value,
                ];
            }
        }
        $where  = [];
        $models = (new ProductCategoryModel())->getProductCategoryList($where);
        return retJson(Lang::GETDone, $models->toArray());
    }

    public function getProductList(Request $request)
    {
        $searchParams = self::getParam([]);
        $where        = [];
        foreach ($searchParams as $key => $value) {
            if (is_array($value) && $key === 'create_time') {
                $where[] = [
                    $key,
                    'between',
                    $value,
                ];
                continue;
            }
            if (strpos($value, '%') !== false) {
                $where[] = [
                    $key,
                    'like',
                    $value,
                ];
            }
            else {
                $where[] = [
                    $key,
                    '=',
                    $value,
                ];
            }
        }
        $where     = [];
        $models    = (new ProductModel())->getProductList($where);
        $modelsArr = $models->toArray();
        self::arrangeProduct($modelsArr, true);
        //        var_dump($modelsArr);die;
        #拓展字段，前台需要
        //
        //        //最大值最小值
        //
        //        //整理图片
        //        $mediaIds = [];
        //        foreach ($modelsArr['data'] as $model) {
        //            foreach ($model['main_media_ids'] as $main_media_id) {
        //                $mediaIds[] = $main_media_id;
        //            }
        //            foreach ($model['desc_media_ids'] as $desc_media_ids) {
        //                $mediaIds[] = $desc_media_ids;
        //            }
        //        }
        //        $mediaIds    = array_unique($mediaIds);
        //        $mediaModels = (new MediaModel())->whereIn('id', $mediaIds)->select();
        //        foreach ($modelsArr['data'] as &$productInfo) {
        //            $prices = [];
        //            $skus   = [];
        //            foreach ($productInfo['skuInfo'] as $skuInfo) {
        //                $prices [] = $skuInfo['price'];
        //                $skus []   = $skuInfo['sku_name'];
        //            }
        //            $productInfo['extend_min_max']   = (float)max($prices) . '~' . (float)min($prices);
        //            $productInfo['extend_sku_names'] = implode('/', $skus);
        //            foreach ($mediaModels as $mediaModel) {
        //                foreach ($productInfo['main_media_ids'] as $main_media_id) {
        //                    if ($main_media_id === $mediaModel->getData('id')) {
        //                        $productInfo['mainMediaInfo'][] = $mediaModel->toArray();
        //                    }
        //                }
        //                foreach ($productInfo['desc_media_ids'] as $desc_media_id) {
        //                    if ($desc_media_id === $mediaModel->getData('id')) {
        //                        $productInfo['descMediaInfo'][] = $mediaModel->toArray();
        //                    }
        //                }
        //            }
        //        }
        return retJson(Lang::GETDone, $modelsArr);
    }
}